// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#pragma once

#include <cobalt-client/cpp/histogram.h>
#include <fs/metrics/cobalt-metrics.h>
#include <fs/metrics/histograms.h>

namespace fs_metrics {

namespace internal {

// Returns the appropiate histogram for an event.
cobalt_client::Histogram<fs_metrics::VnodeMetrics::kHistogramBuckets>* SelectHistogram(
    const Event event, fs_metrics::VnodeMetrics* metrics);

}  // namespace internal

// This class allows clients to create a single filesystem event that creates metrics for both the
// inspect API and Cobalt instrumentation. This is intended to make it easier to measure events for
// tracking through this distinct mechanisms, and should be used for code that already exposes both
// types of metrics.
//
// In the future, this class will be replaced by just using the Inspect API's 'LatencyEvent', and
// Cobalt histograms will be automatically generated by a proxy service tentatively called the
// 'Broker Service.' Clients who do not currently export Cobalt metrics should prefer using this
// Inspect API directly.
class CompositeLatencyEvent {
 public:
  CompositeLatencyEvent() = delete;
  CompositeLatencyEvent(Event event, Histograms* histogram_collection,
                        fs_metrics::VnodeMetrics* metrics);
  CompositeLatencyEvent(const CompositeLatencyEvent&) = delete;
  CompositeLatencyEvent(CompositeLatencyEvent&&) = default;
  CompositeLatencyEvent& operator=(const CompositeLatencyEvent&) = delete;
  CompositeLatencyEvent& operator=(CompositeLatencyEvent&&) = delete;
  ~CompositeLatencyEvent();

  // Resets start for the latency event.
  void Reset();

  // Prevents the latency event from being recorded.
  void Cancel();

  LatencyEvent* mutable_latency_event() { return &inspect_event_; }
  cobalt_client::Histogram<fs_metrics::VnodeMetrics::kHistogramBuckets>* mutable_histogram() {
    return cobalt_histogram_;
  }

 private:
  LatencyEvent inspect_event_;
  cobalt_client::Histogram<fs_metrics::VnodeMetrics::kHistogramBuckets>* cobalt_histogram_;
};

}  // namespace fs_metrics
